package com.techwells.learningkites.adapter;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.techwells.learningkites.R;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.model.CourseCategory;
import com.techwells.learningkites.networkservice.model.Dialogue;
import com.techwells.learningkites.networkservice.model.Test;
import com.techwells.taco.utils.ToastUtils;

import android.R.integer;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.text.TextUtils.EllipsizeCallback;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class TestListAdapter extends BaseAdapter{
	
	private Context mContext;
	private LayoutInflater mInflater;
	private List<Test> mList;
	private int wordPosition;
	private MediaPlayer mp;
	
	public TestListAdapter(Context mContext, List<Test> mList) {
		super();
		this.mContext = mContext;
		mInflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mList = mList;
		this.wordPosition = (int)(Math.random()*4);
		mp = new MediaPlayer();
	}

	

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final CellHolder cellHolder;
        if (convertView == null) {
        	cellHolder = new CellHolder();
        		convertView = mInflater.inflate(R.layout.item_testlist, null);
        		cellHolder.sound = (ImageView) convertView.findViewById(R.id.sound);
        		cellHolder.enLang = (TextView) convertView.findViewById(R.id.en_lang);
        		cellHolder.num = (TextView) convertView.findViewById(R.id.num);
        		cellHolder.wordA = (TextView) convertView.findViewById(R.id.word_a);
        		cellHolder.wordB = (TextView) convertView.findViewById(R.id.word_b);
        		cellHolder.wordC = (TextView) convertView.findViewById(R.id.word_c);
        		cellHolder.wordD = (TextView) convertView.findViewById(R.id.word_d);
        		cellHolder.showAnswer = (CheckBox) convertView.findViewById(R.id.show_answer);
                convertView.setTag(cellHolder);

        } else {
        	cellHolder = (CellHolder) convertView.getTag();
        }        
       
        
        if (mList.get(position).sequence < 10) {
        	cellHolder.num.setText("\t" + mList.get(position).sequence + ".");
		}else {
			cellHolder.num.setText(""+mList.get(position).sequence + ".");
		}
        String enString = mList.get(position).enLang.replaceAll("\r|\n", "");
        String enStr = enString.replace(mList.get(position).word.word, "____");
        String word = mList.get(position).word.word.substring(0, 1).toUpperCase() + mList.get(position).word.word.substring(1);
        String en = enStr.replace(word, "____");
        cellHolder.enLang.setText(en);
        
        if (wordPosition == 0) {
        	cellHolder.wordA.setText("A."+mList.get(position).word.word);
    		cellHolder.wordB.setText("B."+mList.get(position).relatWords.get(0));
    		cellHolder.wordC.setText("C."+mList.get(position).relatWords.get(1));
    		cellHolder.wordD.setText("D."+mList.get(position).relatWords.get(2));
		}else if (wordPosition == 1) {
			cellHolder.wordA.setText("A."+mList.get(position).relatWords.get(0));
    		cellHolder.wordB.setText("B."+mList.get(position).word.word);
    		cellHolder.wordC.setText("C."+mList.get(position).relatWords.get(1));
    		cellHolder.wordD.setText("D."+mList.get(position).relatWords.get(2));
    		cellHolder.wordA.setTag(1);
		}else if (wordPosition == 2) {
			cellHolder.wordA.setText("A."+mList.get(position).relatWords.get(0));
    		cellHolder.wordB.setText("B."+mList.get(position).relatWords.get(1));
    		cellHolder.wordC.setText("C."+mList.get(position).word.word);
    		cellHolder.wordD.setText("D."+mList.get(position).relatWords.get(2));
		}else if (wordPosition == 3) {
			cellHolder.wordA.setText("A."+mList.get(position).relatWords.get(0));
    		cellHolder.wordB.setText("B."+mList.get(position).relatWords.get(1));
    		cellHolder.wordC.setText("C."+mList.get(position).relatWords.get(2));
    		cellHolder.wordD.setText("D."+mList.get(position).word.word);
		}
        
        cellHolder.showAnswer.setChecked(false);
        cellHolder.wordA.setTextColor(mContext.getResources().getColor(R.color.black_title));
		cellHolder.wordB.setTextColor(mContext.getResources().getColor(R.color.black_title));
		cellHolder.wordC.setTextColor(mContext.getResources().getColor(R.color.black_title));
		cellHolder.wordD.setTextColor(mContext.getResources().getColor(R.color.black_title));
        cellHolder.showAnswer.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					String word = mList.get(position).word.word;
					if (word.equals(cellHolder.wordA.getText().toString().substring(2)) ) {
						cellHolder.wordA.setTextColor(mContext.getResources().getColor(R.color.blue_3));
					}else if (word.equals(cellHolder.wordB.getText().toString().substring(2))) {
						cellHolder.wordB.setTextColor(mContext.getResources().getColor(R.color.blue_3));
					}else if (word.equals(cellHolder.wordC.getText().toString().substring(2))) {
						cellHolder.wordC.setTextColor(mContext.getResources().getColor(R.color.blue_3));
					}else if (word.equals(cellHolder.wordD.getText().toString().substring(2))) {
						cellHolder.wordD.setTextColor(mContext.getResources().getColor(R.color.blue_3));
					}
				}else {
					cellHolder.wordA.setTextColor(mContext.getResources().getColor(R.color.black_title));
					cellHolder.wordB.setTextColor(mContext.getResources().getColor(R.color.black_title));
					cellHolder.wordC.setTextColor(mContext.getResources().getColor(R.color.black_title));
					cellHolder.wordD.setTextColor(mContext.getResources().getColor(R.color.black_title));
				}
				
			}
		});
        
        cellHolder.sound.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					if (mp.isPlaying()) {
						mp.stop();
					}
					mp.reset();
					mp.setDataSource(KitesRequestUrl.BASE_URL_IMG + mList.get(position).resource);
					mp.prepare();
					mp.setOnPreparedListener(new OnPreparedListener() {
						
						@Override
						public void onPrepared(MediaPlayer mp) {
							mp.start();
							
						}
					});
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
        
		return convertView;
	}

	
	public static class CellHolder implements Serializable{
    	TextView num,enLang,wordA,wordB,wordC,wordD;
    	ImageView sound;
    	CheckBox showAnswer;
    }


}
